package com.auerdi.ui.detail.cutprice.child;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.auerdi.GlideApp;
import com.auerdi.R;
import com.auerdi.ui.detail.cutprice.child.view.ClockText;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.MyStartList;
import com.example.egghttp.retrofit.beans.goods.BnyOrder;
import com.example.egghttp.retrofit.params.PageRequest;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADCutChildRight extends SupportFragment {

    Unbinder unbinder;
    List<BnyOrder> mOrderList = new ArrayList<>();
    @BindView(R.id.recy)
    RecyclerView recy;

    public static ADCutChildRight newInstance() {

        Bundle args = new Bundle();

        ADCutChildRight fragment = new ADCutChildRight();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_cut_child_b, container, false);

        L.d("hcia", "view:" + view);

        unbinder = ButterKnife.bind(this, view);


        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        L.d("hcia", "onViewCreated:");
        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recy.setAdapter(new CommonAdapter<BnyOrder>(getActivity(), R.layout.item_mybarga, mOrderList) {


            @Override
            protected void convert(ViewHolder holder, final BnyOrder item, final int outposition) {


                ClockText clockText = holder.getView(R.id.time);


                clockText.setCondownSecond(

                        Integer.parseInt(item.getRemaining_time()), Integer.parseInt(item.getTimesta())

                );

//                clockText.setBnyOrder(mOrderList,outposition);


                holder.setText(R.id.time, item.getRemaining_time());
                holder.setText(R.id.start_date_time, item.getStart_date_time());
                holder.setText(R.id.goods_name, item.getGoods_name());
                holder.setText(R.id.title, item.getShort_title());

                ImageView gPic = holder.getView(R.id.gpic);
                ImageView big = holder.getView(R.id.img);

                GlideApp.with(holder.getConvertView().getContext())
                        .load(item.getCover_img_url())
                        .placeholder(R.mipmap.eptb)
                        .error(R.mipmap.eptb)
                        .centerCrop()
                        .into(big);

                GlideApp.with(holder.getConvertView().getContext())
                        .load(item.getCover_img_url())
                        .placeholder(R.mipmap.eptb)
                        .error(R.mipmap.eptb)
                        .centerCrop()
                        .into(gPic);
//
            }
        });

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        L.d("hcia", "onLazyInitView:");


        RetrofitClient.getPDAClient().create(AdService.class).bargainmyBargainLists(new PageRequest("0"))
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<MyStartList>>() {
                               @Override
                               public void onCompleted() {

                               }

                               @Override
                               public void onError(Throwable e) {
                                   L.d("hcia", "e:" + e);

                               }

                               @Override
                               public void onNext(BOb<MyStartList> payWeixinResponceBOb) {
                                   List<BnyOrder> lsss = payWeixinResponceBOb.getBody().getStart_list();

                                   L.d("hcia", "lsss:" + lsss.size());
                                   Long tsLong = System.currentTimeMillis() / 1000;
                                   String ts = tsLong.toString();
                                   for (BnyOrder bnyOrder : lsss) {
                                       bnyOrder.setTimesta(ts);

                                   }

                                   mOrderList.clear();
                                   mOrderList.addAll(lsss);
                                   recy.getAdapter().notifyDataSetChanged();


                               }
                           }
                );


    }


    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
